﻿//Ben Gotthold
// Joe Illuminati
// CISC 360
// 9/23/2013

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CISC_360_project
{
    //  Will enable multiple Processes to be held in CPU.cs instead of just one.
    public class Process : IDisposable
    {
        private List<Instruction> _text;
        public List<Instruction> TEXT { get { return _text; } }
        private byte[][] _binary_instr;
        public byte[][] BINARY { get { return _binary_instr; } }
        private bool _running;
        private int _cycles_ran;
        // Threading
        private Thread _process;
        private AutoResetEvent _process_signal;


        public Process(List<Instruction> text, byte[][] binary_instr)
        {
            this._text = text;
            this._binary_instr = binary_instr;
            this._running = true;
            this._cycles_ran = 0;
            // Threading
            _process = new Thread(incrementCyclesRanThread);
            _process_signal = new AutoResetEvent(false);
        }

        /// <summary>
        /// Kill all the threads running and dispose of the object.
        /// </summary>
        public void Dispose()
        {
            _running = false;
            _process_signal.Set();
            _process.Join();
        }

        /// <summary>
        /// Sends a signal to the threaded method to let it know to update
        /// the number of cycles ran.
        /// </summary>
        public void incrementCyclesRan()
        {
            _process_signal.Set();
        }

        /// <summary>
        /// The threaded method that will increment the # of cycles ran
        /// </summary>
        private void incrementCyclesRanThread()
        {
            while (_running)
            {
                _process_signal.WaitOne();
                _cycles_ran++;
            }
        }
    }
}
